﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public static class SqlHelper
    {
        /// <summary>
        /// 获取数据库连接字符串
        /// </summary>
        public static readonly string conStr = ConfigurationManager.ConnectionStrings["mssqlserver"].ConnectionString;

        /// <summary>
        /// 执行非查询操作
        /// </summary>
        /// <param name="sql">要执行的sql语句</param>
        /// /// <param name="cmdType">要执行的sql语句类型</param>
        /// <param name="pms">sql语句所使用到的参数</param>
        /// <returns></returns>
        public static int ExecuteNonQurey(string sql, CommandType cmdType, params SqlParameter[] pms)
        {
            int i;
            using (SqlConnection con=new SqlConnection (conStr ))
            {
                using (SqlCommand cmd=new SqlCommand (sql,con))
                {

                    if(pms!=null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    i = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    return i;
                }
            }
        }

        /// <summary>
        /// 执行查询，返回一个单行单列的数据
        /// </summary>
        /// <param name="sql">要执行的sql语句</param>
        /// /// <param name="cmdType">要执行的sql语句类型</param>
        /// <param name="pms">sql语句所使用到的参数</param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql,CommandType cmdType, params SqlParameter[] pms)
        {
            object o;
            using (SqlConnection con= new SqlConnection (conStr))
            {
                using (SqlCommand cmd=new SqlCommand (sql,con))
                {
                    if(pms!=null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    o=cmd.ExecuteScalar();
                    cmd.Parameters.Clear();
                    return o;
                }
            }
        }

        /// <summary>
        /// 执行单条数据读取操作
        /// </summary>
        /// <param name="sql">要执行的sql语句</param>
        /// /// <param name="cmdType">要执行的sql语句类型</param>
        /// <param name="pms">sql语句所使用到的参数</param>
        /// <returns></returns>
        public static SqlDataReader SqlDataReader(string sql,CommandType cmdType, params SqlParameter[] pms)
        {
            SqlConnection con = new SqlConnection(conStr);
            using (SqlCommand cmd=new SqlCommand (sql,con))
            {
                if(pms!=null)
                {
                    cmd.Parameters.AddRange(pms);
                }
                try
                {
                    con.Open();
                    return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                }
                catch 
                {
                    con.Close();
                    con.Dispose();
                    throw;
                }
            }
        }

        /// <summary>
        /// 执行数据查询操作，返回一个DataTable
        /// </summary>
        /// <param name="sql">要执行的sql语句</param>
        /// /// <param name="cmdType">要执行的sql语句类型</param>
        /// <param name="pms">sql语句所使用到的参数</param>
        /// <returns>查询出的数据，存放在一个DataTable中</returns>
        public static DataTable SqlDataTable(string sql,CommandType cmdType, params SqlParameter[] pms)
        {
            DataTable dt = new DataTable();
            using (SqlDataAdapter sda = new SqlDataAdapter (sql,conStr))
            {
                if(pms!=null)
                {
                    sda.SelectCommand.Parameters.AddRange(pms);
                }
                sda.Fill(dt);
            }
            return dt;
        }
        /// <summary>
        /// 执行数据分页查询操作，返回一个DataTable
        /// </summary>
        /// <param name="sql">要执行的sql语句</param>
        /// /// <param name="cmdType">要执行的sql语句类型</param>
        /// <param name="recordCount">查询到的数据总数</param>
        /// <param name="pageCount">查询到的数据总页数</param>
        /// <param name="pms">sql语句所使用到的参数</param>
        /// <returns>查询出的分页数据，存放在一个DataTable中</returns>
        public static DataTable SqlDataTable(string sql,CommandType cmdType,out int recordCount,out int pageCount, params SqlParameter[] pms)
        {
            DataTable dt = new DataTable();
            using (SqlDataAdapter sda = new SqlDataAdapter(sql, conStr))
            {
                if (pms != null)
                {
                    sda.SelectCommand.Parameters.AddRange(pms);
                }
                sda.Fill(dt);
            }
            recordCount = (int)pms[2].Value;
            pageCount = (int)pms[3].Value;
            return dt;
        }
    }
}
